package columbia.edu;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.*;
import java.sql.*;

import javax.servlet.*;

public class Registration extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void init(ServletConfig config) throws ServletException{
		super.init(config);
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
		String connectionURL = "jdbc:oracle:thin:@//w4111b.cs.columbia.edu:1521/ADB";
		Connection connection=null;
		res.setContentType("text/html");
		PrintWriter out = res.getWriter();
		String uname = req.getParameter("username");
		String pass = req.getParameter("password");
		String fname = req.getParameter("firstname");
		String lname = req.getParameter("lastname");
		String street = req.getParameter("street");
		String city = req.getParameter("city");
		String state = req.getParameter("state");
		String country = req.getParameter("country");
		String zipcode = req.getParameter("zipcode");
		String shipstreet = req.getParameter("shipstreet");
		String shipcity = req.getParameter("shipcity");
		String shipstate = req.getParameter("shipstate");
		String shipcountry = req.getParameter("shipcountry");
		String shipzipcode = req.getParameter("shipzipcode");
		String cardname = req.getParameter("card");
		String cardno = req.getParameter("cardno");
		String expdate = req.getParameter("expdate");
		
		System.out.println("ship street "+ shipstreet);

		String addid = "1";
		String addidship = "10";
		String payid = "1";
		String typeaddr = "B";
		String shiptypeaddr = "S";
		String typemember= "R";
		
		try {
			Class.forName("oracle.jdbc.pool.OracleDataSource");
			connection = (Connection) req.getSession().getAttribute("Connection");
			if (connection == null) {
				connection = DriverManager.getConnection(connectionURL, "ab3416", "kFxJssQk");
				req.getSession().setAttribute("Connection", connection);
			}
		      
			
			System.out.println("conn est");
			
			String sqlcust = "insert into customers (cust_id,pwd,fname,lname,type_of_member) values(?,?,?,?,?)";
			String sqladd = "insert into address values (?,?,?,?,?,?,?,?)";
			String sqladdship = "insert into address values (?,?,?,?,?,?,?,?)";
			String sqlpay = "insert into payment_info values (?,?,?,?,?)";

			
			PreparedStatement pstcust = connection.prepareStatement(sqlcust);
			PreparedStatement pstadd = connection.prepareStatement(sqladd);
			PreparedStatement pstaddship = connection.prepareStatement(sqladdship);
			PreparedStatement pstpay = connection.prepareStatement(sqlpay);
			
			pstcust.setString(1,uname );
			pstcust.setString(2,pass );
			pstcust.setString(3,fname );
			pstcust.setString(4,lname );
			pstcust.setString(5,typemember );
			pstcust.executeUpdate();
			pstcust.close();
			
			System.out.println("insert in cust  complete");

			
			pstadd.setString(1, addid);
			pstadd.setString(2, typeaddr);
			pstadd.setString(3, street);
			pstadd.setString(4, city);
			pstadd.setString(5, state);
			pstadd.setString(6, country);
			pstadd.setString(7, zipcode);
			pstadd.setString(8, uname);
			pstadd.executeUpdate();
			pstadd.close();
			
			System.out.println("insert in address bill complete");		
			
			pstaddship.setString(1, addidship);
			pstaddship.setString(2, shiptypeaddr);
			pstaddship.setString(3, shipstreet);
			pstaddship.setString(4, shipcity);
			pstaddship.setString(5, shipstate);
			pstaddship.setString(6, shipcountry);
			pstaddship.setString(7, shipzipcode);
			pstaddship.setString(8, uname);
			pstaddship.executeUpdate();
			pstaddship.close();
						
			
			System.out.println("insert in address ship complete");

			
			pstpay.setString(1, payid);
			pstpay.setString(2, cardname);
			pstpay.setString(3, cardno);
			pstpay.setString(4, expdate);
			pstpay.setString(5, uname);
			pstpay.executeUpdate();
			pstpay.close();
			
			System.out.println("insert in pay  complete");
			
			System.out.println("insert complete");
			
		}
		catch(ClassNotFoundException e){
			out.println("Couldn't load database driver: " + e.getMessage());
		}
		catch(SQLException e){
		out.println("SQLException caught: " + e.getMessage());
		}
		catch (Exception e){
		out.println(e);
		}
		finally {
			req.setAttribute("fname", fname.toUpperCase() );
			String nextJSP = "/index2.jsp";
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
			dispatcher.forward(req,res);
/*
		try {
		if (connection != null) connection.close();
		}
		catch (SQLException ignored){
		out.println(ignored);
		}
*/		}
	}
}